package com.android.widget.gridview;

import com.android.widget.demo.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

public class GridViewSimple extends Activity {
	private final static String[] Number = {"1","2","3","4","5","6"
											,"7","8","9","10","11","12"
											,"13","14","15","16","17","18"
											,"19","20","21","22","23","24"
											,"25","26","27","28","29","30"
											,"31","32","33","34","35","36"
											,"37","38","39","40","41","42"
											,"43","44","45","46","47","48"
											,"49","50","51","52","53","54"
											,"55","56","57","58","59","60"
											,"61","62","63","64","65","66"
											};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gridview_simple);
		initView();
	}
	
	private void initView() {
		GridView gridview = (GridView)findViewById(R.id.gridview);
		SimpleGridAdapter adapter = new SimpleGridAdapter(Number, this);
		gridview.setAdapter(adapter);
	}
	
	private class SimpleGridAdapter extends BaseAdapter {
		private String[] mVideoCountList;
		private LayoutInflater mInflater;
		
		public SimpleGridAdapter(String[] list,Context context)
		{
			mVideoCountList = list;
			mInflater = LayoutInflater.from(context);
		}
		
		public int getCount() {
			return mVideoCountList.length;
		}

		public Object getItem(int position) {
			return mVideoCountList[position];
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(final int position, View convertView, ViewGroup parent ) {
		        final ViewHolder viewHolder; 
		        if (convertView == null) 
		        { 
		            convertView = mInflater.inflate(R.layout.grid_item, null); 
		            viewHolder = new ViewHolder(); 
		            viewHolder.btnVideoCount = (Button) convertView.findViewById(R.id.btn_number);
		            convertView.setTag(viewHolder); 
		        } else
		        { 
		            viewHolder = (ViewHolder) convertView.getTag(); 
		        } 
		        final String videoCount = mVideoCountList[position];
		        viewHolder.btnVideoCount.setText(videoCount);
		        viewHolder.btnVideoCount.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						
					}
		        	
				});
		        return convertView; 
		}
	}
	private static class ViewHolder
	{
		public Button btnVideoCount;
	}
}
